const WXAPI = require('apifm-wxapi')
Page({
  data: {
    footer_img: wx.getStorageSync('footer_img'),
    sphid: wx.getStorageSync('sphid'),
    index_video1: wx.getStorageSync('index_video1'),
    index_img0: wx.getStorageSync('index_img0'),
    index_img1: wx.getStorageSync('index_img1'),
    index_img2: wx.getStorageSync('index_img2'),
    index_img3: wx.getStorageSync('index_img3'),
    defaultBanners: [],
    category: [],
    new: [],
  },
  onLoad: function (options) {
    const that = this;
    that.setData({
      footer_img: wx.getStorageSync('footer_img'),
      sphid: wx.getStorageSync('sphid'),
      index_video1: wx.getStorageSync('index_video1'),
      index_img0: wx.getStorageSync('index_img0'),
      index_img1: wx.getStorageSync('index_img1'),
      index_img2: wx.getStorageSync('index_img2'),
      index_img3: wx.getStorageSync('index_img3'),
    });
    that.getBanner();
    that.getCategory();
    that.getNew();
  },
  onShow: function () {

  },

  // 获取幻灯片
  getBanner() {
    const that = this;
    WXAPI.banners({
      type: 'banner'
    }).then(function (res) {
      if (res.code == 700) {
        wx.showModal({
          title: '提示',
          content: '请在后台添加 banner 轮播图片，自定义类型填写 banner',
          showCancel: false
        })
      } else {
        wx.setStorageSync('njbanner', res.data);
        that.setData({
          defaultBanners: res.data
        });
      }
    }).catch(function (e) {
      wx.showToast({
        title: res.msg,
        icon: 'none'
      })
    })
  },

  // 获取分类
  async getCategory() {
    var that = this;
    await WXAPI.goodsCategory().then(function (res) {
      let categories = [];
      if (res.code == 0) {
        for (var i = 0; i < res.data.length; i++) {
          if (res.data[i].pid == "418591" && res.data[i].type == 1) {
            categories.push(res.data[i]);
          }
        }

        that.setData({
          category: categories,
        });

      }
    })

  },

  // 获取最新作品
  async getNew() {
    var that = this;
    await WXAPI.goods({
      page: 1,
      pageSize: 8,
      // orderBy: 'addedDown',
    }).then(function (res) {
      if (res.code == 0) {
        that.setData({
          new: res.data
        });
      }
    })
  },

  tovideoPost: function () {
    wx.navigateTo({
      url: "/pages/a1_video_list/index"
    })
  },
  
  //跳转到列表页面
  toList: function (t) {
    var a = t.currentTarget.dataset.categoryid;
    wx.navigateTo({
      url: "/pages/a1_list/index?categoryId=" + a
    })
  },

  //跳转到详情页面
  toPost: function (t) {
    var a = t.currentTarget.dataset.postid;
    wx.navigateTo({
      url: "/pages/a1_post/index?id=" + a
    })
  },

  //跳转到a2页面
  toA2: function () {
    wx.switchTab({
      url: '/pages/a2/index',
    });
  },



  // 点击幻灯片
  gotoBannerLink: function (t) {
    var a = t.currentTarget.dataset.link;
    console.info("open link", a), a && (a.includes("https://mp.weixin.qq.com")) && wx.navigateTo({
      url: "/pages/webview/webview?link=" + a
    });
  },

  //拨打电话
  callPhone() {
    wx.makePhoneCall({
      phoneNumber: wx.getStorageSync('phone')
    })
  },

  // 打开视频号主页
  toVideo() {
    wx.openChannelsUserProfile({
      finderUserName: wx.getStorageSync('sphid'),
    });
  },


  onShareAppMessage() { },
  onShareTimeline() { },
})